package com.distribute.framework.dao.impl;

import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.core.web.util.Page;
import com.distribute.framework.core.util.Function;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.distribute.framework.core.sql.mongodb.MongodbDaoSupport;
import com.distribute.framework.core.sql.mongodb.MongodbDataSource;

import org.springframework.stereotype.Repository;
import javax.annotation.Resource;

import com.distribute.framework.plugin.*;
import com.distribute.framework.dao.ISrvLogDao;
import com.distribute.framework.model.SrvLog;



/**
 * 系统日志的Dao实现
 * @author 飞啊飘啊
 * 感谢mongodb的nosql框架
 */
@SuppressWarnings("unchecked")
@Repository(value="srvLogDao")
public class SrvLogDao extends MongodbDaoSupport<SrvLog> implements ISrvLogDao
{

	@Resource(name="distributeDataDataSource")
	MongodbDataSource dataSource;
	@Override
	public MongodbDataSource getDataSource() {
		return dataSource;
	}
	

	public Class getTClass() { return SrvLog.class; }
	/**
	 * 索引信息
	 */
	public Map<String, Integer> getTIndex() {
		Map<String, Integer> indexes = new HashMap();
		//indexes.put("slId",-2);
		//indexes.put("slHost",1);
		//indexes.put("slModel",1);
		//indexes.put("slType",1);
		//indexes.put("slUser",1);
		/************LAZY3Q_DEFINE_DAO_INDEX************/
		/************LAZY3Q_DEFINE_DAO_INDEX************/
		return indexes;
	}
	/************LAZY3Q_DEFINE_CODE************/
	/************LAZY3Q_DEFINE_CODE************/

	/**
	 * 自定义加工处理params,可以添加表别名、联表查询等操作
	 * 可在此动态设置数据源，以达到分库分表的目的
	 * @param params
	 * @return
	 */
	private Map custom(Map params){
		/************LAZY3Q_DAO_CUSTOM************/
		/************LAZY3Q_DAO_CUSTOM************/
		return params;
	}
	
	/**
	 * 自定义加工处理params,可以添加表别名、联表查询等操作
	 * 可在此动态设置数据源，以达到分库分表的目的
	 * @param params
	 * @return
	 */
	private SrvLog custom(SrvLog params){
		/************LAZY3Q_CUSTOM_MODEL************/
		/************LAZY3Q_CUSTOM_MODEL************/
		return params;
	}
	/************LAZY3Q_DEFINE_FUNCTION************/
	/************LAZY3Q_DEFINE_FUNCTION************/
	

	/**
	 * 获取所有系统日志
	 * @return
	 */	
	public List<SrvLog> getSrvLogList(Map params,Integer offset,Integer size)
	{		
		params = custom(params);
		List<SrvLog> list = this.queryForList(params, offset, size);
		return list;
	}
	
	
	/**
	 * 获取系统日志统计
	 * @return
	 */
	public List getSrvLogStats(Map params,Map<String,Object> groups,Map<String,Object> sums)
	{
		throw new RuntimeException("mongo统计功能未实现");
	}

	/**
	 * 获取系统日志分页
	 * @return
	 */
	public Page getSrvLogPage(int page,int size,Map params)
	{
		params = custom(params);
		
		if(size<1)size=20;
		
		//获取分页总数
		Long count=getSrvLogCount(params);
		//取分页的队列
		List list=this.queryForList(params, (int)Page.test(page,size,count), size);
		//返回一个包装分页对象
		return new Page(page,size,count,list);
	}
	
	/**
	 * 获取系统日志数量
	 * @return
	 */	
	public Long getSrvLogCount(Map params)
	{
		params = custom(params);
		return this.queryCount(params);
	}
	
	/**
	 * 获取系统日志自定义求和表达式,比如求ID平均值:eval="avg(id)"
	 * @return
	 */	
	public <T> T getSrvLogEval(String eval,Map params)
	{
		throw new RuntimeException("mongo获取eval功能未实现");
	}
	
	
	/**
	 * 根据查询条件取系统日志
	 * @param params
	 * @return
	 */
	public SrvLog getSrvLog(Map params)
	{		
		params = custom(params);
			
		List<SrvLog> list=this.getSrvLogList(params,0,1);
		SrvLog srvLog = list.isEmpty()?null:list.get(0);
		
		return srvLog;
	}
		
	/**
	 * 根据ID取系统日志
	 * @param id
	 * @return
	 */
	public SrvLog getSrvLogById(Long id)
	{
		if(id==null)
			return null;
		
		Map params=new HashMap();
		params.put("slId", id);
		params = custom(params);
		return (SrvLog) this.queryForObject(params);
	}
	
	/**
	 * 根据ID取系统日志(不缓存)
	 * @param id
	 * @return
	 */
	public SrvLog GetSrvLogById(Long id){
		return  getSrvLogById(id);
	}
	

	/**
	 * 保存系统日志
	 * @param srvLog
	 */
	public SrvLog saveSrvLog(SrvLog srvLog)
	{
		srvLog = custom(srvLog);
		Trigger.on(SrvLog.class,Trigger.BEFOR,Trigger.SAVE,srvLog);
		srvLog = this.insert(srvLog);
		return Trigger.on(SrvLog.class,Trigger.AFTER,Trigger.SAVE,srvLog);
	}

	

	/**
	 * 更新系统日志
	 * @param srvLog
	 */
	public int updateSrvLog(SrvLog srvLog)
	{
		srvLog = custom(srvLog);
		Trigger.on(SrvLog.class,Trigger.BEFOR,Trigger.UPDATE,srvLog);
		int rows = this.update(srvLog);
		Trigger.on(SrvLog.class,Trigger.AFTER,Trigger.UPDATE,srvLog);
		return rows;
	}
	/**
	 * 更新系统日志非空字段
	 * @param srvLog
	 */
	public int updateSrvLog$NotNull(SrvLog srvLog)
	{
		srvLog = custom(srvLog);
		Trigger.on(SrvLog.class,Trigger.BEFOR,Trigger.UPDATE_NOTNULL,srvLog);
		int rows = this.update(srvLog);
		Trigger.on(SrvLog.class,Trigger.AFTER,Trigger.UPDATE_NOTNULL,srvLog);
		return rows;
	}

	/**
	 * 根据ID删除一个系统日志
	 * @param id
	 */
	public void deleteSrvLogById(Long id)
	{
		Map params=new HashMap();
		params.put("slId", id);
		Trigger.on(SrvLog.class,Trigger.BEFOR,Trigger.DELETE,params);
		params = custom(params);
		Trigger.on(SrvLog.class,Trigger.AFTER,Trigger.DELETE,params);
		this.delete(params);
		
	}
	/**
	 * 根据条件更新系统日志
	 * @param values
	 * @param params
	 */
	public int updateSrvLogByConfirm(Map values, Map params){
		params = custom(params);
		Trigger.on(SrvLog.class,Trigger.BEFOR,Trigger.UPDATE_BY,params);
		int rows = this.update(params, values);
		Trigger.on(SrvLog.class,Trigger.AFTER,Trigger.UPDATE,params);
		return rows;
	}
	/**
	 * 根据条件删除系统日志
	 * @param params
	 */
	public void deleteSrvLogByConfirm(Map params){
		params = custom(params);
		Trigger.on(SrvLog.class,Trigger.BEFOR,Trigger.DELETE_BY,params);
		this.delete(params);
		Trigger.on(SrvLog.class,Trigger.AFTER,Trigger.DELETE_BY,params);
	}
	

	/**
	 * 事务处理
	 */
	public <T> T executeTransaction(Function function) {
		return function.execute();	
	}

}